﻿using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WeChat.Common;

namespace WeChat.UserInfo
{
    public partial class WaterCtrlReservatedCode : System.Web.UI.Page
    {
        WaterReserveBLL wrb = new WaterReserveBLL();
        public string rfkh = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                //认证后得到code值
                if (Request.QueryString["code"] != null || Request.QueryString["code"] != "")
                {
                    string code = Request.QueryString["code"];
                    if (Session["openid"] == null || Session["openid"].ToString() == "")
                    {
                        try
                        {
                            Session["openid"] = OAuthHelper.getOpenid(code);
                        }
                        catch (Exception ex)
                        {

                            ApplicationLog.Write("WaterCtrlReservatedCode获取openid异常信息=" + ex.Message);
                        }
                        
                    }
                    string openid = Session["openid"].ToString();
                     
                    if (!new BindIdBLL().verifyId(openid))//验证是否绑定账号
                    {
                        Response.Redirect("/Binding.aspx?openid=" + openid);
                    }
                    else //默认查询7天内预约记录
                    {
                        GetUserInfoBLL gb = new GetUserInfoBLL();
                        DataTable dt = gb.getUserInfo(openid).Tables[0];
                         rfkh = dt.Rows[0]["rfkh"].ToString();
                        this.Session["rfkh"] = rfkh;
                        if ("管理卡".Equals(dt.Rows[0]["ryxm"].ToString()) || dt.Rows[0]["rfkh"].ToString() == "000001")
                        {
                            Response.Redirect("SYSCount.aspx?openid=" + openid);
                        }
                        else
                        {
                            string startTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                            string endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                            //dateslive.Text = "7天内预约记录";
                            dataBinds(openid, startTime, endTime);
                        }
                    }
                }
            }



        }



        //days事件，查询记录
        protected void days(object sender, EventArgs e)
        {
            HtmlAnchor htmla = (HtmlAnchor)sender;
            string id = htmla.ID;//选择查询分类
            string startTime = "";//查询开始时间
            string endTime = "";//查询最后时间
            string openid = Session["openid"].ToString();
            switch (id)
            {
                case "A1"://当天时间
                    startTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
                case "A2"://七天内时间
                    startTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                case "A3"://30天内时间
                    startTime = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                default://全部记录
                    startTime = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
            }
            //dateslive.Text = htmla.InnerText;
            dataBinds(openid, startTime, endTime);
        }

        //查询信息
        public void dataBinds(string openids, string startTime, string endTime)
        {

            DataSet ds = wrb.getReservatedCode(openids, startTime, endTime);
            cdcatalog.DataSource = ds;
            cdcatalog.DataBind();
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    tb1.Visible = true;

                   // costs.Text = wrb.getConsumCount(openids, startTime, endTime) == "" ? "0.00" : Double.Parse(gb.getConsumCount(openids, startTime, endTime)).ToString("f2");
                }
                else
                {
                    tb1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Response.Write("错误：" + ex.Message);
            }
        }







    }
}